﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace virtuaPol.Net
{
    /// <summary>
    /// Interaction logic for MainWnd.xaml
    /// </summary>
    public partial class MainWnd : Window
    {
        private bool IsFullScreen { get; set; }

        private List<Option> options = new List<Option>();

        public MainWnd()
        {
            virtuaPol.Util.CLogger.Start("virtuaPol.net.log");
            Application.Current.Exit += new ExitEventHandler(Application_Exit);
            InitializeComponent();
            LoadOptionsList();
            IsFullScreen = true;

            options.Add(new Option("New Game", new EventHandler(NewGame_Clicked)));
            options.Add(new Option("Load Game", new EventHandler(LoadGame_Clicked)));
            options.Add(new Option("Options", new EventHandler(Options_Clicked)));
            options.Add(new Option("Exit", new EventHandler(Exit_Clicked)));
        }

        void Application_Exit(object sender, ExitEventArgs e)
        {
            virtuaPol.Util.CLogger.Stop();
        }

        private void LoadOptionsList()
        {
            OptionsList.ItemsSource = options;
        }

        private void ToggleFullscreen()
        {
            if (IsFullScreen)
            {
                WindowStyle = WindowStyle.SingleBorderWindow;
                Topmost = false;
            }
            else
            {
                WindowStyle = WindowStyle.None;
                Topmost = true;
                WindowState = WindowState.Maximized;
                Width = System.Windows.SystemParameters.PrimaryScreenWidth;
                Height = System.Windows.SystemParameters.PrimaryScreenHeight;
                
            }
            IsFullScreen = !IsFullScreen;
        }

        public void NewGame_Clicked(object sender, EventArgs e)
        {
            NewGameDlg newGameDlg = new NewGameDlg();
            newGameDlg.ShowDialog();
            if (newGameDlg.IsCanceled)
                return;
            GameWnd gameWnd = new GameWnd(newGameDlg.World);
            gameWnd.Show();
            gameWnd.Focus();
            this.Close();
        }

        public void LoadGame_Clicked(object sender, EventArgs e)
        {
            LoadGameDlg loadGameDlg = new LoadGameDlg();
            loadGameDlg.ShowDialog();
            if (loadGameDlg.IsCanceled)
                return;
            GameWnd gameWnd = new GameWnd(loadGameDlg.World);
            gameWnd.Show();
            gameWnd.Focus();
            this.Close();
        }

        public void Options_Clicked(object sender, EventArgs e)
        {
            MessageBox.Show("This function is not yet implemented.", 
                "Not yet implemented!", 
                MessageBoxButton.OK, MessageBoxImage.Information);
        }

        public void Exit_Clicked(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        /// <summary>
        /// Handle all key events
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            Key key = Key.None;
            if (e.Key == Key.System)
                key = e.SystemKey;
            else
                key = e.Key;
            switch (key)
            {
                default:
                    return;
                case Key.Q:
                    // If the left Alt key is pressed, quit the application
                    if (e.KeyboardDevice.IsKeyDown(Key.LeftAlt))
                        Application.Current.Shutdown();
                    break;
                case Key.F1:
                    // Open Documentation
                    
                    break;
                case Key.Enter:
                    // If left Alt key is pressed, toggle fullscreen
                    if (e.KeyboardDevice.IsKeyDown(Key.LeftAlt))
                        ToggleFullscreen();
                    break;
            }
            e.Handled = true;
        }

        private void OptionsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                Option o = (Option)e.AddedItems[0];
                //MessageBox.Show(String.Format("The option '{0}' was selected!", o.Name));
                o.OnClick(sender, new EventArgs());
                OptionsList.UnselectAll();
            }
        }
    }
}
